package rs.mbs2.epics.server.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import rs.mbs2.epics.server.entity.Album;
import rs.mbs2.epics.server.entity.Comment;
import rs.mbs2.epics.server.entity.Photo;
import rs.mbs2.epics.server.entity.User;
import rs.mbs2.epics.server.session.dao.localinterface.UserDaoLocal;

@SuppressWarnings("serial")
public class LastCommentsController extends HttpServlet {

	@EJB
	private UserDaoLocal userDao;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String idString = request.getParameter("id");
		Integer id = Integer.parseInt(idString);
		User user = userDao.findById(id);
		
		List<Comment> comments = new ArrayList<>();
		for(Album album : user.getAlbums()) {
			for(Photo photo : album.getPhotos()) {
				for(Comment comment : photo.getComments()) {
					if(!comment.getUser().equals(user)) {	// trebaju nam komentari drugi korisnika
						comments.add(comment);
					}
				}
			}
		}
		
		request.setAttribute("viewedUser", user);
		request.setAttribute("comments", comments);
		getServletContext().getRequestDispatcher("/viewRecentComments.jsp").forward(request, response);
		return;
		
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
}
